package com.drondea.testclient.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 配置信息内置缓存
 * @author shd
 */
public class ConfigMemory {

    private static final ConcurrentHashMap<String, Map> CONFIG_MAP = new ConcurrentHashMap<>();

    public static <T> void put(String key, Map<String, T> value) {
        CONFIG_MAP.put(key, value);
    }

    public static <T> Map<String, T> get(String key, Class<T> clazz) {
        return CONFIG_MAP.get(key);
    }

    public static <T> T getBean(String type, String key, Class<T> clazz) {
        return (T) CONFIG_MAP.get(type).get(key);
    }

    public static <T> void put(String type, String key, T value) {
        if (CONFIG_MAP.get(type) != null) {
            CONFIG_MAP.get(type).put(key, value);
        } else {
            Map map = new HashMap<String, T>();
            map.put(key, value);
            CONFIG_MAP.put(type, map);
        }
    }

    public static <T> void delete(String type, String key) {
        if (CONFIG_MAP.get(type) != null) {
            CONFIG_MAP.get(type).remove(key);
        }
    }

    public static ConcurrentHashMap getAll() {
        return CONFIG_MAP;
    }
}
